#include "TicTacToe.h"
#include <conio.h>
using namespace Course;


TicTacToe::TicTacToe(void)
{
	this->turnNotOver = true;
	this->winner = 5;
	this->gameOver = false;

	for( int y=0; y<3; y++ )
	{
		for( int x=0; x<3; x++ )
		{
			this->grid[y][x] = ' ';
		}
	}

}


TicTacToe::~TicTacToe(void)
{
}

void TicTacToe::drawBoard( int x, int y)
{
	Console::clrscr();
	Console::gotoxy(0,0);
	cout << " - - - ";
	Console::gotoxy(0,1);
	cout << "|" << this->grid[0][0] << "|" << this->grid[0][1] << "|" << this->grid[0][2] << "|";
	Console::gotoxy(0,2);
	cout << "|-|-|-|";
	Console::gotoxy(0,3);
	cout << "|" << this->grid[1][0] << "|" << this->grid[1][1] << "|" << this->grid[1][2] << "|";
	Console::gotoxy(0,4);
	cout << "|-|-|-|";
	Console::gotoxy(0,5);
	cout << "|" << this->grid[2][0] << "|" << this->grid[2][1] << "|" << this->grid[2][2] << "|";
	Console::gotoxy(0,6);
	cout << " - - - ";
	Course::Console::gotoxy(x,y);
}

void TicTacToe::moveCursor( int input )
{

	if( input == 72 ) // Up
	{
		if( Console::wherey() > 2 )
		{
			Console::gotoxy( Console::wherex(), Console::wherey() - 2 );
		}
	}

	if( input == 77 ) // Right
	{
		if( Console::wherex() < 4 )
		{
			Console::gotoxy( Console::wherex() + 2, Console::wherey() );
		}
	}

	if( input == 75 ) // Left
	{
		if( Console::wherex() > 2 )
		{
			Console::gotoxy( Console::wherex() - 2, Console::wherey() );
		}
	}

	if( input == 80 ) // Down
	{
		if( Console::wherey() < 4 )
		{
			Console::gotoxy( Console::wherex(), Console::wherey() + 2 );
		}
	}
		
}

void TicTacToe::drawMark( int input, int& turn )
{
	if( input == 13 )
		{
			int y = ((Course::Console::wherey() -1) /2 );
			int x = ((Course::Console::wherex() -1) /2 );

			if( this->grid[y][x] == ' ' )
			{
				if( turn == 0 )
				{
					this->grid[y][x] = 'X';
					this->turnNotOver = false;
					this->drawBoard(Course::Console::wherex(), Course::Console::wherey());
					turn = 1;
					
				}
				else
				{
					this->grid[y][x] = 'O';
					this->turnNotOver = false;
					this->drawBoard(Course::Console::wherex(), Course::Console::wherey());
					turn = 0;
				}
			}
		}
}

char TicTacToe::detectWinner( int winType )
{
	return this->grid[winType][winType];	
}

int TicTacToe::detectGameOver( )
{
	// Check if winner on rows
	for( int i=0; i<3; i++ )
	{
		if( (this->grid[i][0] == this->grid[i][1]) && (this->grid[i][1] == this->grid[i][2]) && (this->grid[i][0] != ' ') )
		{
			this->gameOver = true;
			return i;
		}
	}

	// Check if winner on columns
	for( int i=0; i<3; i++ )
	{
		if( (this->grid[0][i] == this->grid[1][i]) && (this->grid[1][i] == this->grid[2][i]) && (this->grid[i][0] != ' ') )
		{
			this->gameOver = true;
			return i;
		}
	}

	// Check if winner on diagonals

	if( (this->grid[0][0] == this->grid[1][1]) && (this->grid[1][1] == this->grid[2][2]) && (this->grid[0][0] != ' ') )
	{
		this->gameOver = true;
		return 1;
	}
	if( (this->grid[2][0] == this->grid[1][1]) && (this->grid[1][1] == this->grid[0][2]) && (this->grid[0][0] != ' ') )
	{
		this->gameOver = true;
		return 1;
	}

	return 5;
}

void TicTacToe::printWinner( char winner )
{
	Console::clrscr();
	Console::gotoxy(10,5);
	cout << "Congratulations " << winner << ". You win!!" << endl;
}